home *** CD-ROM | disk | FTP | other *** search
- /***** BEGIN LICENSE BLOCK *****
- - Version: MPL 1.1/GPL 2.0/LGPL 2.1
- -
- - The contents of this file are subject to the Mozilla Public License Version
- - 1.1 (the "License"); you may not use this file except in compliance with
- - the License. You may obtain a copy of the License at
- - http://www.mozilla.org/MPL/
- -
- - Software distributed under the License is distributed on an "AS IS" basis,
- - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
- - for the specific language governing rights and limitations under the
- - License.
- -
- - The Original Code is "FlashGot".
- -
- - The Initial Developer of the Original Code is Giorgio Maone.
- - Portions created by the Initial Developer are Copyright (C) 2004
- - the Initial Developer. All Rights Reserved.
- -
- - Contributor(s): Giorgio Maone <g.maone @ informaction.com>
- -
- - Alternatively, the contents of this file may be used under the terms of
- - either the GNU General Public License Version 2 or later (the "GPL"), or
- - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
- - in which case the provisions of the GPL or the LGPL are applicable instead
- - of those above. If you wish to allow use of your version of this file only
- - under the terms of either the GPL or the LGPL, and not to allow others to
- - use your version of this file under the terms of the MPL, indicate your
- - decision by deleting the provisions above and replace them with the notice
- - and other provisions required by the LGPL or the GPL. If you do not delete
- - the provisions above, a recipient may use your version of this file under
- - the terms of any one of the MPL, the GPL or the LGPL.
- -
- - ***** END LICENSE BLOCK *****/
-
-
-
- function FlashGot() {
-
- try {
-
- this.log("Per-window init started");
- // install listeners
- const contextMenu=document.getElementById("contentAreaContextMenu");
- if(contextMenu) {
- contextMenu.addEventListener("popupshowing", function(ev) {
- flashGot.menuShowing(ev)
- }, false);
- }
-
- window.addEventListener("mouseover", function(ev) {
- flashGot.hoverElement=ev.target;
- }, false);
-
- } catch(err) {
- this.log("Unrecoverable init error: "+err.message);
- }
-
- flashGot=this;
-
- this.log("Per-window init done");
- }
-
- FlashGot.prototype = {
- lastHoverElement: null
- ,
- log: function(msg) {
- g_flashGotService.log(msg);
- }
- ,
- switchOption: function(opt) {
- g_flashGotService.setPref(opt,!g_flashGotService.getPref(opt));
- }
- ,
- prepareMenus: function() {
- function menuSwitch(name, disabled) {
- var menuItem=document.getElementById("flashgot-menuitem-"+name);
- if(menuItem && ! ( menuItem.hidden = hidden ) ) {
- menuItem.setAttribute("disabled",disabled?"true":"false");
- menuItem.hidden=disabled && hideDisabled;
- }
- }
- const defaultDM=g_flashGotService.defaultDM;
-
- const invalidLink=!this.popupLink;
-
- const invalidSel= this.srcWindow.getSelection().isCollapsed;
- // [check me, was:]
- // const selText=this.srcWindow.getSelection().toString();
- // const invalidSel= ! (selText.length>0 && /[^\s]+/.test(selText));
-
- const noLink=this.linksCount==0;
- const hideDisabled=g_flashGotService.getPref("hideDisabledCmds");
-
- var hidden=!g_flashGotService.DMS.found;
- menuSwitch("it", invalidLink || defaultDM.disabledLink);
- menuSwitch("sel", invalidSel || defaultDM.disabledSel);
- menuSwitch("all", noLink || defaultDM.disabledAll);
-
- hidden=false;
- menuSwitch("buildGallery",invalidLink && invalidSel && noLink);
-
- const optsMenu=document.getElementById("flashgot-menu-options");
- const opts=optsMenu.getElementsByTagName("menuitem");
-
- var menuItem,match;
- var j=opts.length;
- while(j-->0) {
- menuItem=opts[j];
- match=menuItem.id?menuItem.id.match(/opt-(.*)/):null;
- if(match) {
- menuItem.setAttribute("checked",
- g_flashGotService.getPref(match[1])?"true":"false"
- );
- }
- }
-
- const dms=g_flashGotService.DMS;
- var dm,menuItemId;
- var anchorNode=document.getElementById("flashgot-menusep-dms");
- for(j=dms.length; j-->0;) {
- dm=dms[j];
- menuItemId="flashgot-menuopt-dm-"+dm.name;
- menuItem=document.getElementById(menuItemId);
- if(!menuItem) {
- menuItem=document.createElement("menuitem");
- menuItem.setAttribute("id",menuItemId);
- menuItem.setAttribute("name","flashgot-menuopt-dm");
- menuItem.setAttribute("type","radio");
- menuItem.setAttribute("autocheck","true");
- menuItem.setAttribute("oncommand","g_flashGotService.defaultDM=\""+dm.name+"\"");
- menuItem.setAttribute("label",dm.name);
- anchorNode.parentNode.insertBefore(menuItem,anchorNode);
- }
- menuItem.setAttribute("checked",(dm.supported && defaultDM==dm.name)?"true":"false");
- menuItem.setAttribute("disabled",dm.supported?"false":"true");
- anchorNode=menuItem;
- }
-
- }
- ,
- menuShowing: function(ev) {
- this.prepareMenus();
- }
- ,
- get srcWindow() {
- return document.commandDispatcher.focusedWindow;
- }
- ,
- get srcDocument() {
- return this.srcWindow.document;
- }
- ,
- get popupLink() {
- return g_flashGotService.findLinkAsc(document.popupNode);
- }
- ,
- get linksCount() {
- const doc=this.srcDocument;
- var count=doc.links.length;
- if(g_flashGotService.getPref("includeImages")) count+=doc.images.length;
- return count;
- }
- ,
- getLinks: function(filter,includeImages) {
- var allLinks=filterLinks(this.srcDocument.links,filter);
- const urlResolver=Components.classes['@mozilla.org/network/io-service;1'].getService(
- Components.interfaces.nsIIOService).newURI(this.srcDocument.location.href,null,null);
-
- if(includeImages) {
- allLinks=allLinks.concat(filterLinks(
- this.srcDocument.images,
- function(l) {
- l.href=urlResolver.resolve(l.src)
- if(!l.title) l.title=l.alt+" ("+l.width+","+l.height+")";
- return filter(l);
- }));
- }
-
- allLinks=allLinks.concat(filterLinks(
- this.srcDocument.getElementsByTagName("embed"),
- function(l) {
- l.href=urlResolver.resolve(l.src);
- if(!l.title) l.title=l.alt?l.alt:l.type;
- return filter(l);
- }));
- return allLinks;
-
- function filterLinks(allLinks,filter) {
- const links=new Array();
- for(var j=0,len=allLinks.length; j<len; j++) {
- var l=allLinks[j];
- if(filter(l)) {
- links[links.length]=l;
- }
- }
- return links;
- }
- }
- ,
- get referrer() {
- if(this._referrer) return this._referrer;
- var doc=this.srcDocument;
- var ref=doc._flashgotGB_referrer;
- return ref?ref:doc.location.href;
- }
- ,
- set referrer(r) {
- return this._referrer=r;
- }
- ,
- getSelectionLinks: function(includeImages) {
- var sel=this.srcWindow.getSelection();
-
- this.selection=sel;
- var links=this.getLinks(function(link) {
- return g_flashGotService.checkLink(link) && flashGot.selection.containsNode(link,true);
- }, includeImages);
- const httpFixer=/^h.{2}p/i;
- const noprotFixer=/^([a-z]+[a-z\d]+\.[a-z\d\.]+\/)/i;
- const m=this.selection.toString().match(/\b(h.{2}p:\/\/|ftp:\/\/|mms:\/\/|rtsp:\/\/|[a-z]+[a-z\d]+\.[a-z\d\.]+\/)[^\s]*/gi);
- if(m) {
- var textLinks=new Array();
- for(var j=0, len=m.length; j<len; j++) {
- var href=m[j].replace(httpFixer,"http").replace(noprotFixer,"http://$1");
- for(var k=links.length; k-->0;) {
- if(links[k].href.toLowerCase()==href.toLowerCase()) {
- href=null; break;
- }
- }
- if(href) {
- textLinks[textLinks.length]={href: href, title: m[j], innerHTML: "", tagName: "flashgottextlink"};
- }
- }
- links=links.concat(textLinks);
- }
- return links;
- }
- ,
- downloadPopupLink: function(dmName) {
- const link=this.popupLink;
- if(link != null) {
- this.download([link], dmName);
- }
- }
- ,
- downloadLink: function(dmName) {
- const sel=this.srcWindow.getSelection();
- this.log("hover: "+this.hoverElement);
- var link=g_flashGotService.checkAnchor(sel.anchorNode)
- ?sel.anchorNode
- :(link=g_flashGotService.findLinkAsc(this.hoverElement))?link:null
- ;
- this.log("link: "+link);
- if(link) {
- this.download([link], g_flashGotService.OP_ONE, dmName);
- }
- }
- ,
- downloadSel: function(dmName) {
- this.download(this.getSelectionLinks(g_flashGotService.getPref("includeImages")),
- g_flashGotService.OP_SEL,dmName);
- }
- ,
- downloadAll: function(dmName) {
- const links=this.getLinks(
- g_flashGotService.checkLink,
- g_flashGotService.getPref("includeImages"));
- this.download(links, g_flashGotService.OP_ALL, dmName);
- }
- ,
- download: function(links, opType, dmName) {
- links.document=this.srcDocument;
- links.window=this.srcWindow;
- links.referrer=this.referrer;
- links.cookie=links.document.cookie;
- return g_flashGotService.download(links, opType, dmName);
- }
- ,
- buildGallery: function() {
- var previewURL=null, contentURL=null;
- var doc=this.srcDocument;
- if(doc._flashgotGB_previews || doc._flashgotGB_contents) {
- previewURL=doc._flashgotGB_previews;
- contentURL=doc._flashgotGB_contents;
- } else {
- var links=this.getSelectionLinks(true);
- if(links.length==0) {
- links=this.popupLink
- ?[this.popupLink]
- :this.srcDocument.links;
- }
-
- var previewRX=/\d+.*\.(jpg|jpeg|png|gif|bmp)(\?|$)/i;
- var contentRX=/\d+.*\.[a-z]{2,4}(\?|$)/i;
- for(var j=0, len=links.length; j<len && ! (contentURL && previewURL); j++) {
- var l=links[j];
- var tag=l.tagName.toLowerCase();
- var url=l.href;
- if( (!previewURL) && (tag=="img" || previewRX.test(url)) ) {
- previewURL=url;
- } else {
- if(tag!="img" && contentRX.test(url)) {
- contentURL=url;
- if(tag=="a") {
- var imgs=l.getElementsByTagName("img");
- for(var i=0; i<imgs.length; i++) {
- if(previewRX.test(imgs[i].src)) {
- previewURL=imgs[i].src;
- break;
- }
- }
- }
- }
- }
- }
- if(!previewURL) previewURL="";
- if(previewURL && !contentURL) contentURL=previewURL;
- }
- window.openDialog("chrome://flashgot/content/flashgotGalleryBuilder.xul","_blank",
- "chrome,dialog,centerscreen",
- {
- previewURL: previewURL,
- contentURL: contentURL,
- referrerURL: this.referrer,
- originalWindow: window,
- srcDocument: this.srcDocument,
- tmpDir: g_flashGotService.tmpDir
- }
- );
- }
-
- }
-
-
-
- window.addEventListener("load",
- function(loadEv) {
- new FlashGot();
- }, false);
-
-
-
-